package procilege.impl.formatter;

import java.util.Collection;
import java.util.Iterator;

import procilege.compute.ExprFormatTraveller;
import procilege.compute.PlaceTypeMetadata;
import procilege.compute.PlaceType;
import procilege.lang.EntityExprElement;
import procilege.lang.EntityExprOr;

@PlaceTypeMetadata(type = EntityExprOr.class)
public class EntityExprOrFormatter implements PlaceType<ExprFormatTraveller> {

	public void visit(ExprFormatTraveller tr, Object place) {

		StringBuilder sb = tr.getBuilder();

		Collection<EntityExprElement> terms = ((EntityExprOr) place).getTerms();
		if (terms.isEmpty())
			return;

		Iterator<EntityExprElement> it = terms.iterator();

		EntityExprElement t = it.next();

		if (it.hasNext()) {

			sb.append("(");

			tr.setout(t);

			while (it.hasNext()) {

				t = it.next();
				sb.append("||");
				tr.setout(t);
			}

			sb.append(")");
		} else {

			if (t != null)
				tr.setout(t);
			else
				sb.append("null");
		}

	}
}